<?php

require_once('global.php');
require_once(R_P . 'require/init.php');
$rid = intval($_GET['rid']);


require_once(D_P . 'data/cache/server.php');
$rs = $db->get_one("SELECT * FROM cms_resource WHERE rid='$rid'");
$cid = intval($rs['cid']);
if (in_array($cid, $cate_allow))
{
	$filepath = $rs['filepath'];
	$root = $server[$rs['serverid']]['serverpath'];
	$url = $server[$rs['serverid']]['serverurl'];
	$url = trim($url);
	$filesize = $rs['filesize'];

	if ($filesize >= 50000000) //50M以外就直接下载
	{
		header("Location:$url$filepath");
		exit;
	}
	else
	{
		$fileext = $rs['type'];
		$name = $rs['filename'];
		$name = mb_convert_encoding($name, 'gb2312', 'utf-8');
		//$name = iconv('utf-8', 'gb2312', $name);
		$path = $root . $filepath;
		//$filesize = filesize($path);
		if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')!==false && $fileext=='torrent' ){
			$attachment='inline';
		} else{
			$attachment='attachment';
		}
		$ctype = '';
		switch($fileext)
		{
			case "pdf"	: $ctype = "application/pdf"; break;
			case "rar"	:
			case "zip"	: $ctype = "application/zip"; break;
			case "doc"	: $ctype = "application/msword"; break;
			case "xls"	: $ctype = "application/vnd.ms-excel"; break;
			case "ppt"	: $ctype = "application/vnd.ms-powerpoint"; break;
			case "gif"	: $ctype = "image/gif"; break;
			case "png"	: $ctype = "image/png"; break;
			case "jpeg"	:
			case "jpg"	: $ctype = "image/jpeg"; break;
			case "wav"	: $ctype = "audio/x-wav"; break;
			case "mpeg"	:
			case "mpg"	:
			case "mpe"	: $ctype = "video/x-mpeg"; break;
			case "mov"	: $ctype = "video/quicktime"; break;
			case "avi"	: $ctype = "video/x-msvideo"; break;
			case "txt"	: $ctype = "text/plain"; break;
			default		: $ctype = "application/octet-stream";
		}
		ob_end_clean();
		header('Last-Modified: '.gmdate('D, d M Y H:i:s',$timestamp+86400).' GMT');
		header('Expires: '.gmdate('D, d M Y H:i:s',$timestamp+86400).' GMT');
		header('Cache-control: max-age=86400');
		header('Content-Encoding: none');
		header("Content-Disposition: $attachment; filename=\"$name\"");
		header("Content-type: $ctype");
		header("Content-Transfer-Encoding: binary");
		$filesize && header("Content-Length: $filesize");
		readfile($path);
		exit;
	}
}
else
{
	start();
	exit('您所在的用户组无权下载此文件');
}
?>